﻿namespace Huayou.Common
{
    using Huayou.Common.Database;
    using System;
    using System.Data;

    public class CommonBaseDB
    {
        private DBOperate curDBOperater;
        private const int ERRORSQL = -1;
        private const int MSACCESS = 2;
        private const int MSSQLSERVER = 1;
        private const int PROCEDURESQL = 1;
        private const int SELECTSQL = 2;
        private const int UPDATESQL = 0;

        public CommonBaseDB(DBOperate dbOper)
        {
            this.curDBOperater = dbOper;
        }

        public void BeginTran()
        {
            this.curDBOperater.BeginTran();
        }

        public void Close()
        {
            this.curDBOperater.Close();
        }

        public void CommitTran()
        {
            this.curDBOperater.CommitTran();
        }

        public IDataReader ExecuteForDataReader(string strSql)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return null;

                case 2:
                    return this.curDBOperater.ExecQueryForDataReader(strSql);

                case 1:
                    return this.curDBOperater.ExecProcForDataReader(strSql, null);
            }
            return null;
        }

        public IDataReader ExecuteForDataReader(string strSql, IDataParameter[] parameters)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return null;

                case 2:
                    return this.curDBOperater.ExecPreQueryForDataReader(strSql, parameters);

                case 1:
                    return this.curDBOperater.ExecProcForDataReader(strSql, parameters);
            }
            return null;
        }

        public DataSet ExecuteForDataSet(string strSql, string strTableName)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return null;

                case 2:
                    return this.curDBOperater.ExecQueryForDataSet(strSql, strTableName);

                case 1:
                    return this.curDBOperater.ExecProcForDataSet(strSql, null, strTableName);
            }
            return null;
        }

        public DataSet ExecuteForDataSet(string strSql, IDataParameter[] parameters, string strTableName)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return null;

                case 2:
                    return this.curDBOperater.ExecPreQueryForDataSet(strSql, parameters, strTableName);

                case 1:
                    return this.curDBOperater.ExecProcForDataSet(strSql, parameters, strTableName);
            }
            return null;
        }

        public DataTable ExecuteForDataTable(string strSql)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return null;

                case 2:
                    return this.curDBOperater.ExecQueryForDataTable(strSql);

                case 1:
                    return this.curDBOperater.ExecProcForDataTable(strSql, null);
            }
            return null;
        }

        public DataTable ExecuteForDataTable(string strSql, IDataParameter[] parameters)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return null;

                case 2:
                    return this.curDBOperater.ExecPreQueryForDataTable(strSql, parameters);

                case 1:
                    return this.curDBOperater.ExecProcForDataTable(strSql, parameters);
            }
            return null;
        }

        public int ExecuteForInt(string strSql)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return -1;

                case 0:
                    return this.curDBOperater.ExecUpdateSql(strSql);

                case 1:
                    return this.curDBOperater.ExecUpdateProc(strSql, null);
            }
            return -1;
        }

        public int ExecuteForInt(string strSql, IDataParameter[] parameters)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return -1;

                case 0:
                    return this.curDBOperater.ExecPreUpdateSql(strSql, parameters);

                case 1:
                    return this.curDBOperater.ExecUpdateProc(strSql, parameters);
            }
            return -1;
        }

        public object ExecuteForScalar(string strSql)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return -1;

                case 2:
                    return this.curDBOperater.ExecScalar(strSql);

                case 1:
                    return this.curDBOperater.ExecScalar(strSql, null);
            }
            return -1;
        }

        public object ExecuteForScalar(string strSql, IDataParameter[] parameters)
        {
            switch (this.GetSqlType(strSql))
            {
                case -1:
                    return null;

                case 2:
                    return this.curDBOperater.ExecScalar(strSql, parameters);

                case 1:
                    return this.curDBOperater.ExecScalar(strSql, parameters);
            }
            return null;
        }

        private int GetSqlType(string strSql)
        {
            string str = "";
            if ((strSql == null) || (strSql.Length == 0))
            {
                return -1;
            }
            if (strSql.Length > 7)
            {
                str = strSql.Substring(0, 7).ToUpper();
                if ((((str.Equals("UPDATE ") || str.Equals("INSERT ")) || (str.Equals("DELETE ") || str.Equals("ALTER T"))) || (str.Equals("ALTER  ") || str.Equals("BACKUP "))) || str.Equals("RESTORE"))
                {
                    return 0;
                }
                if (str.Equals("SELECT "))
                {
                    return 2;
                }
                return 1;
            }
            return 1;
        }

        public void Open()
        {
            this.curDBOperater.Open();
        }

        public void RollBackTran()
        {
            this.curDBOperater.RollBackTran();
        }
    }
}

